package com.huawei.appgallery.foundation.ui.framework.widget;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Scroller;
import com.bumptech.glide.Glide;
import com.huawei.appgallery.foundation.ui.framework.cardkit.widget.CardListAdapter;
import com.huawei.appmarket.framework.widget.ExpandableLayout;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import o.cig;
import o.cij;
import o.cix;
import o.djh;
import o.dky.j;
import o.Logger;
import o.dok;
import o.dyk;
import o.epv;

public class PullUpListView extends ListView implements OnScrollListener, cig {
    private boolean ʻ = true;
    private OnScrollListener ʼ = null;
    private boolean ʽ = false;
    public Scroller ˊ;
    private boolean ˊॱ = false;
    protected FooterView ˋ = null;
    private int ˋॱ = -1;
    protected a ˎ = null;
    protected cij ˏ = null;
    private int ˏॱ = -1;
    private boolean ͺ;
    protected boolean ॱ = false;
    private CardListAdapter ॱˊ;
    private int ॱˋ = 1;
    private boolean ॱˎ = false;
    private int ॱॱ = 0;
    private boolean ॱᐝ = false;
    private boolean ᐝ = true;
    private boolean ᐝॱ = false;

    public interface a {
        void ʼᐝ();

        void ʽᐝ();

        void ʿॱ();

        void ˈॱ();

        void ˊʼ();
    }

    public static class SavedState extends BaseSavedState {
        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            public /* synthetic */ Object createFromParcel(Parcel parcel) {
                return ॱ(parcel);
            }

            public /* synthetic */ Object[] newArray(int i) {
                return ˎ(i);
            }

            public SavedState ॱ(Parcel parcel) {
                return new SavedState(parcel);
            }

            public SavedState[] ˎ(int i) {
                return new SavedState[i];
            }
        };
        private int ˊ = 0;
        private boolean ˎ = false;

        public SavedState(Parcelable parcelable) {
            super(parcelable);
        }

        public SavedState(Parcel parcel) {
            super(parcel);
            this.ˎ = ((Boolean) parcel.readValue(null)).booleanValue();
            this.ˊ = ((Integer) parcel.readValue(null)).intValue();
        }

        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeValue(Boolean.valueOf(this.ˎ));
            parcel.writeValue(Integer.valueOf(this.ˊ));
        }

        public int ˏ() {
            return this.ˊ;
        }

        public void ˊ(int i) {
            this.ˊ = i;
        }

        public String toString() {
            return "PullUpListView.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " loadingFailed=" + this.ˎ + "}";
        }
    }

    public PullUpListView(Context context) {
        super(context);
        ˊ(context);
    }

    public PullUpListView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        ˊ(context);
    }

    public PullUpListView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        ˊ(context);
    }

    protected void ˊ(Context context) {
        this.ˊ = ॱ(context);
        this.ˊॱ = dok.ᐝ(context);
        super.setOnScrollListener(this);
    }

    protected Scroller ॱ(Context context) {
        return new Scroller(context, new DecelerateInterpolator());
    }

    public void setEnableChangeLoadingView(boolean z) {
        this.ᐝ = z;
    }

    public void setLoadingListener(a aVar) {
        this.ˎ = aVar;
    }

    public synchronized boolean ॱॱ() {
        return this.ॱ;
    }

    public synchronized void setmPullRefreshing(boolean z) {
        this.ॱ = z;
    }

    public int ᐝ() {
        return this.ˏॱ;
    }

    public void setCurrScrollState(int i) {
        this.ˏॱ = i;
    }

    public void computeScroll() {
        if (this.ˊ.computeScrollOffset()) {
            postInvalidate();
        }
        super.computeScroll();
    }

    public void setAdapter(ListAdapter listAdapter) {
        ʼ();
        if (listAdapter instanceof cij) {
            this.ˏ = (cij) listAdapter;
            if (getFooterViewsCount() == 0 && ॱˋ()) {
                this.ˋ = new FooterView(getContext());
                addFooterView(this.ˋ);
                if (!this.ˏ.hasMore()) {
                    ˏॱ();
                }
            }
        }
        if (listAdapter instanceof CardListAdapter) {
            ((CardListAdapter) listAdapter).setRefreshListener(this);
            this.ॱˊ = (CardListAdapter) listAdapter;
        }
        super.setAdapter(listAdapter);
    }

    public void ʽ() {
        ᐝॱ();
        setSelection(0);
        if (this.ʼ != null) {
            this.ʼ.onScrollStateChanged(this, 0);
        }
    }

    private void ᐝॱ() {
        try {
            Field declaredField = AbsListView.class.getDeclaredField("mFlingRunnable");
            declaredField.setAccessible(true);
            Method declaredMethod = declaredField.getType().getDeclaredMethod("endFling", new Class[0]);
            declaredMethod.setAccessible(true);
            declaredMethod.invoke(declaredField.get(this), new Object[0]);
        } catch (Throwable e) {
            Logger.ˋ("PullUpListView", "listView stop exception: ", e);
        }
    }

    protected void ʼ() {
        if (this.ˋ != null) {
            removeFooterView(this.ˋ);
        }
    }

    public void ʻ() {
        if (this.ˋ != null) {
            this.ˋ.ˏ();
        }
    }

    public void ॱˊ() {
        if (this.ˋ == null) {
            Logger.ˋ("PullUpListView", "loadingFailed, mFooterView == null");
        } else {
            this.ˋ.ˊ(getResources().getString(j.ॱˋ), new OnClickListener(this) {
                final /* synthetic */ PullUpListView ॱ;

                {
                    this.ॱ = r1;
                }

                public void onClick(View view) {
                    if (this.ॱ.ˎ != null) {
                        this.ॱ.ˋ.ˏ(2);
                        this.ॱ.ˎ.ʼᐝ();
                    }
                }
            });
        }
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.ʼ = onScrollListener;
    }

    public void ͺ() {
        if (this.ˋ != null) {
            this.ˋ.ˎ();
        }
    }

    public View ˊॱ() {
        return this.ˋ;
    }

    public void ˏॱ() {
        if (this.ˋ != null) {
            this.ʽ = false;
            this.ˋ.ˋ();
            Logger.ˎ("PullUpListView", "hideFooterView");
        }
    }

    public void ˋॱ() {
        if (this.ˋ != null) {
            this.ʽ = true;
            this.ˋ.ॱ();
            Logger.ˎ("PullUpListView", "showFooterView");
        }
    }

    public void ॱˎ() {
        if (this.ˋ != null) {
            this.ˋ.ᐝ();
        }
    }

    public void setFooterViewListener(cix o_cix) {
        if (this.ˋ != null) {
            this.ˋ.setFootViewListener(o_cix);
        }
    }

    public void setBlankViewHeight(int i) {
        if (this.ˋ != null) {
            this.ˋ.setBlankHeight(i);
        }
    }

    public void onScrollStateChanged(AbsListView absListView, int i) {
        setCurrScrollState(i);
        ॱ(i);
        if (!(ॱॱ() || this.ॱˎ || this.ˋ == null || !this.ˏ.hasMore() || i != 0 || getLastVisiblePosition() < this.ॱॱ - 4 || this.ˎ == null || this.ˋ.ˊ() == 2)) {
            this.ˋ.ˏ(2);
            ʽॱ();
            this.ˎ.ʽᐝ();
        }
        if (this.ʼ != null) {
            this.ʼ.onScrollStateChanged(absListView, i);
        }
    }

    protected void handleDataChanged() {
        super.handleDataChanged();
        if (this.ˋ == null) {
            Logger.ˋ("PullUpListView", "handleDataChanged, mFooterView = null");
            return;
        }
        if (this.ᐝ) {
            this.ˋ.ˏ(0);
        }
        ॱ();
        if (!this.ˏ.hasMore()) {
            ˏॱ();
            y_();
        }
    }

    protected void ॱ() {
    }

    protected void y_() {
    }

    public void onScroll(AbsListView absListView, int i, int i2, int i3) {
        if (this.ᐝॱ) {
            ʼॱ();
        }
        this.ॱॱ = i3;
        int footerViewsCount = getFooterViewsCount() + getHeaderViewsCount();
        if (this.ˏ != null && this.ˏ.hasMore() && i2 != 0 && i3 > footerViewsCount && i3 <= i2) {
            ˎ();
        } else if (this.ˏॱ != -1) {
            ʽॱ();
            this.ͺ = i + i2 == i3;
            if (this.ʼ != null) {
                this.ʼ.onScroll(absListView, i, i2, i3);
            }
        }
    }

    private void ʽॱ() {
        if (!this.ॱˎ && !this.ʽ && this.ˏ != null && this.ˏ.hasMore()) {
            ˋॱ();
        }
    }

    private void ʼॱ() {
        int headerViewsCount;
        int i = this.ˋॱ / this.ॱˋ;
        if (getAdapter() instanceof HeaderViewListAdapter) {
            headerViewsCount = i + getHeaderViewsCount();
        } else {
            headerViewsCount = i;
        }
        for (int i2 = 0; i2 < getChildCount(); i2++) {
            ExpandableLayout expandableLayout = (ExpandableLayout) getChildAt(i2).findViewWithTag("expandable");
            if (expandableLayout != null) {
                if (i2 != headerViewsCount - getFirstVisiblePosition()) {
                    expandableLayout.ˎ();
                }
                if (i2 == headerViewsCount - getFirstVisiblePosition()) {
                    expandableLayout.ˋ();
                }
            }
        }
    }

    protected void ˎ() {
        if (this.ˋ == null) {
            Logger.ˋ("PullUpListView", "doExec, mFooterView = null");
        } else if (!this.ˏ.hasMore()) {
            ˏॱ();
        } else if (this.ˋ.ˊ() != 2) {
            this.ˋ.ˏ(2);
            if (this.ˎ != null) {
                Logger.ˎ("PullUpListView", "doExec, data not full screen, onLoadingMore()");
                this.ˎ.ʽᐝ();
            }
        }
    }

    public Parcelable onSaveInstanceState() {
        Object savedState = new SavedState(super.onSaveInstanceState());
        if (this.ˋ == null) {
            Logger.e("PullUpListView", "onSaveInstanceState, mFooterView = null");
        } else if (this.ˋ.ˎ) {
            savedState.ˎ = true;
        }
        return savedState;
    }

    public void onRestoreInstanceState(Parcelable parcelable) {
        if (parcelable instanceof SavedState) {
            SavedState savedState = (SavedState) parcelable;
            super.onRestoreInstanceState(savedState.getSuperState());
            if (savedState.ˎ) {
                ॱˊ();
                return;
            }
            return;
        }
        super.onRestoreInstanceState(parcelable);
    }

    protected void layoutChildren() {
        try {
            super.layoutChildren();
        } catch (Exception e) {
            Logger.e("PullUpListView", "layoutChildren, execption: " + e.toString());
        }
    }

    public boolean ॱˋ() {
        return this.ʻ;
    }

    public void setNeedFootView(boolean z) {
        this.ʻ = z;
    }

    public void setInterceptScrollOnTop(boolean z) {
        this.ॱᐝ = z;
    }

    public void setInterceptScrollOnBottom(boolean z) {
        this.ॱˎ = z;
    }

    protected boolean overScrollBy(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, boolean z) {
        int i9;
        if (this.ॱᐝ && i2 < 0 && ʻॱ() && !ॱᐝ()) {
            i2 = 0;
        }
        if (this.ॱˎ && r12 > 0 && i4 >= 0 && ʻॱ() && ॱᐝ()) {
            i2 = 0;
        }
        if (this.ˋ == null || this.ˋ.ˊ() == 3 || !ॱᐝ()) {
            i9 = i2;
        } else {
            i9 = 0;
        }
        return super.overScrollBy(i, i9, i3, i4, i5, i6, i7, i8, z);
    }

    public boolean ʻॱ() {
        return !canScrollVertically(-1);
    }

    public boolean ॱᐝ() {
        return !canScrollVertically(1);
    }

    private void ॱ(int i) {
        if (!this.ˊॱ) {
            return;
        }
        if (i == 0) {
            Glide.ˏ(getContext()).ˊ();
        } else {
            Glide.ˏ(getContext()).ˏ();
        }
    }

    public void ˎ(int i) {
        if (this.ˋ != null) {
            this.ˋ.ˊ(i);
        } else {
            Logger.ˋ("PullUpListView", "showNoMoreTip mFooterView null");
        }
    }

    public boolean performItemClick(View view, int i, long j) {
        int headerViewsCount;
        int i2 = this.ˋॱ;
        this.ˋॱ = i;
        int i3 = i / this.ॱˋ;
        if (getAdapter() instanceof HeaderViewListAdapter) {
            headerViewsCount = i3 + getHeaderViewsCount();
        } else {
            headerViewsCount = i3;
        }
        for (int i4 = 0; i4 < getChildCount(); i4++) {
            ExpandableLayout expandableLayout;
            if (getFirstVisiblePosition() + i4 != headerViewsCount) {
                expandableLayout = (ExpandableLayout) getChildAt(i4).findViewWithTag("expandable");
                if (expandableLayout != null) {
                    expandableLayout.ˏ();
                }
            }
        }
        i3 = headerViewsCount - getFirstVisiblePosition();
        if (getChildAt(i3) == null) {
            return false;
        }
        expandableLayout = (ExpandableLayout) getChildAt(i3).findViewWithTag("expandable");
        if (expandableLayout != null) {
            expandableLayout.ˊ();
            ॱ(headerViewsCount, i2, expandableLayout);
        }
        return super.performItemClick(view, i, j);
    }

    public void ˋ() {
        int ॱ = djh.ˎ().ॱ();
        if (this.ˋॱ != ॱ) {
            this.ᐝॱ = true;
            this.ॱˊ.notifyDataSetChanged();
            ʾ();
            performItemClick(null, ॱ, 0);
            this.ˋॱ = ॱ;
            new dyk((ListView) this).ॱ();
        }
    }

    public void ˏ() {
        this.ˋॱ = djh.ˎ().ॱ();
        this.ᐝॱ = true;
        ʾ();
    }

    private void ʾ() {
        if (epv.ˏॱ(getContext())) {
            this.ॱˋ = 2;
        } else {
            this.ॱˋ = 1;
        }
    }

    private void ॱ(int i, int i2, View view) {
        view.measure(MeasureSpec.makeMeasureSpec(0, 0), MeasureSpec.makeMeasureSpec(0, 0));
        final int measuredHeight = view.getMeasuredHeight();
        final View view2 = view;
        final int i3 = i;
        final int i4 = i2;
        view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener(this) {
            final /* synthetic */ PullUpListView ˏ;

            public boolean onPreDraw() {
                int i;
                view2.getViewTreeObserver().removeOnPreDrawListener(this);
                int[] iArr = new int[2];
                view2.getLocationOnScreen(iArr);
                int[] iArr2 = new int[2];
                this.ˏ.getLocationOnScreen(iArr2);
                int measuredHeight = iArr2[1] + this.ˏ.getMeasuredHeight();
                if (i3 < i4 || i4 == -1) {
                    i = iArr[1] + measuredHeight;
                } else {
                    i = iArr[1];
                }
                if (i > measuredHeight) {
                    this.ˏ.smoothScrollBy(i - measuredHeight, 300);
                }
                return true;
            }
        });
    }
}
